package com.hboxs.asl.controller.app.index;

import com.hboxs.asl.Filter;
import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Ad;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.service.AdPositionService;
import com.hboxs.asl.service.AdService;
import com.hboxs.asl.service.ProductService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xxx on 2016/12/16
 * 每日新鲜
 */
@Controller("appFreshController")
@RequestMapping("/app/fresh")
public class DailyFreshController extends BaseController {

    protected Logger logger = Logger.getLogger(getClass().getName());

    @Resource(name = "productServiceImpl")
    private ProductService productService;
    @Resource(name = "adServiceImpl")
    private AdService adService;
    @Resource(name = "adPositionServiceImpl")
    private AdPositionService adPositionService;

    /**
     * 每日新鲜商品列表
     *
     * @param pageable 分页
     * @return json
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public
    @ResponseBody
    Object index(Pageable pageable) {
        try {
            List<Ad> ads = adService.findListByAdPosition("每日新鲜广告");

            pageable.getFilters().add(Filter.eq("evFresh", Boolean.TRUE));
            Page page = productService.findPageGroup(
                    null, null, null, null, null, null, null, true, null, null, null, null, Product.OrderType.dateDesc, pageable
            );

            return JsonMessage.success(

                    "ads", JsonMessage.optimiseObj(ads, "id", "path", "url"),

                    "totalPage", page.getTotalPages(),

                    "pageNumber", page.getPageNumber(),

                    "pageSize", page.getPageSize(),

                    "total", page.getTotal(),

                    "content", JsonMessage.optimiseObj(
                            page.getContent(),
                            "id", "image", "name", "price", "promotionPrice", "finalScore", "weight", "sales"
                    )
            );
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
